<?php
namespace app\controller;

use app\BaseController;
use app\service\UserService;
use think\exception\ValidateException;
use think\Request;


class User extends BaseController
{

    protected $userService;

    public function __construct()
    {
        $this->userService = new UserService();
    }

    // 处理登录请求
    public function login(Request $request)
    {
        try{
            // 从请求头中获取 Authorization
            $authHeader = $request->header('Authorization');
            if ($authHeader && stripos($authHeader, 'Bearer ') === 0) {
                $token = substr($authHeader, 7); // 去掉 Bearer 
                if($this->userService->isLogin($token))
                    return json([
                        'code' => 200,
                        'msg'  => '已登录',
                        'data' => $this->userService->getUser($token)
                    ]);
            }

            if ($request->isPost()) {
                $username = $request->param('username');
                $password = $request->param('password');
                validate(\app\validate\User::class)->scene("login")->batch(true)->check([
                    "username" => $username,
                    "password" => $password
                ]);
                $result = $this->userService->login($username, $password);
                    return $result;
            }
        }catch(ValidateException $v){
            return json([
                'code' => 400,
                'msg' => $v->getError()
            ]);
        }
        
        return json(['code' => 400, 'msg' => '请求方法不允许']);
    }

    // 处理注册请求
    public function register(Request $request)
    {
        if ($request->isPost()) {
            // 获取表单数据
            $username        = $request->param('username');
            $password        = $request->param('password');
            $confirmPassword = $request->param('confirm_password');
            $result          = $this->userService->register($username, $password, $confirmPassword);
            return $result;
        }
        return json([
            'code' => 400,
            'msg'  => '请求方法不允许',
        ]);
    }

    // 处理登出请求
    public function logout(Request $request)
    {        
        // 从请求头中获取 Authorization
        $authHeader = $request->header('Authorization');
        $token = substr($authHeader, 7); // 去掉 Bearer 

        if($this->userService->isLogin($token)){
            return  $this->userService->logout($token);
        }
        return json([
            'code' => 400,
            'msg'  => '请先登录'
        ]);
    }

    public function updatePassword(Request $request)
    {
        if ($request->isPost()) {
            $username    = $request->param('username');
            $password    = $request->param('password');
            $newPassword = $request->param("newPassword");
            $result = $this->userService->updatePassword($username, $password, $newPassword);
            return $result;
        } else {
            return json([
                'code' => 400,
                'msg'  => '请求方法不允许',
            ]);
        }
    }

    public function vail(){
        try {
            validate(\app\validate\User::class)->check([
                "username" => input("post.username"),
                "password" => input("post.password"),
                "email" => input("post.email"),
            ]);
        } catch (ValidateException $v) {
            // 验证失败 
            dump($v->getError()); // 输出错误信息
            dump($v->getKey()); // 验证错误的字段名
        }
    }





    
}
